

const blogDetail = new Vue({
    el: '#blog_detail',
    data: {
        title: '',
        content: '',
        ctime: '',
        tags: '',
        views: ''    
    },
    computed: {

    },
    created: function () {  
        const searcheUrlParams = location.search.indexOf("?") > -1 ? location.search.split("?")[1].split("&"): "";
        if (searcheUrlParams == "") {
            return;
        }
        let bid = -2;
        for (var i = 0; i < searcheUrlParams.length; i ++) {
            if (searcheUrlParams[i].split("=")[0] == "bid"){
                try {
                    bid = searcheUrlParams[i].split("=")[1];
                }catch (e) {
                    console.log(e)
                }
            }
            console.log(searcheUrlParams, searcheUrlParams.length)
        }
        axios({
            method: 'get',
            url:"/queryBlogById?bid=" + bid
        }).then((res) => {
            const temp = res.data.data[0];
            this.title = temp.title;
            this.content = temp.content;
            this.tags = temp.tags;
            this.ctime = temp.utime;
            this.views = temp.views;
        }).catch((res) => {
            console.log("请求失败")
        })
    }
})


const send_comment = new Vue({
    el: '#send_comment',
    data: {
        name:'',
        email:'',
        content:'',
        replay:-1,
        relay:0,
        ctime:'',
        utime:'',
        codeImg:'',
        codeVal:'',
        codeText:'',

    },
    methods: {
        
    },
    computed:{
        changeCode:function() {
            return function () {
                axios({
                    method: 'get',
                    url:"/queryRandomCode"
                }).then((res) => {
                    console.log(res)
                    this.codeImg = res.data.data.data;
                    this.codeText = res.data.data.text;
                })
            }
        },
        sendComment:function() {
            return function () {
                const searcheUrlParams = location.search.indexOf("?") > -1 ? location.search.split("?")[1].split("&"): "";
                if (searcheUrlParams == "") {
                    return;
                }
                let bid = -2;
                for (var i = 0; i < searcheUrlParams.length; i ++) {
                    if (searcheUrlParams[i].split("=")[0] == "bid"){
                        try {
                            bid = searcheUrlParams[i].split("=")[1];
                        }catch (e) {
                            console.log(e)
                        }
                    }
            
                }
                const name = this.name;
                const email = this.email;
                const content = this.content;
                const parent = this.replay;
                const parentName = this.relay;
                if(this.codeVal != this.codeText) {
                    alert("验证码有误");
                    return;
                } else{

                    axios({
                        method:'get',
                        url: "/insertComment?bid=" + bid + "&parent=" + parent + "&user_name=" + name + "&email=" + email + "&content=" + content +  "&parent_name=" + parentName
                    }).then(() => {
                        axios({
                            method: "get",
                            url: "/queryCommentsByBlogId?bid=" + bid
                        }).then((res) => {
                            let temp = [];
                            temp = res.data.data;
                            console.log(temp)
                            blogComments.commentList = temp;
                            for(var i = 0 ; i < blogComments.commentList.length; i++ ) {
                                if (blogComments.commentList[i].parent > -1) {
                                    blogComments.commentList[i].options = "回复@" + blogComments.commentList[i].parent_name
                                }
                            }
                        })
                
                        axios({
                            method: "get",
                            url: "/queryBlogCommentsCount?bid=" + bid
                        }).then((res) => {
                            blogComments.total = res.data.data[0].count
                        })
                    })
                }

                
            }
        }
        
        
    },
    created: function () {
        this.changeCode();
    }
})

const blogComments = new Vue({
    el: "#blog_comments",
    data: {
        total: 100,
        commentList: [
            {id:"1", name: "Eric", comments: "你好啊，hello", ctime: "12343", options: ""},
        
        ]
    },
    computed: {
        relay:function (){
            return function (commentId, userName) {
                send_comment.replay = commentId;
                send_comment.relay = userName;
                location.href = "#send_comment"
            }
        }
    },
    created: function () {
        const searcheUrlParams = location.search.indexOf("?") > -1 ? location.search.split("?")[1].split("&"): "";
        if (searcheUrlParams == "") {
            return;
        }
        let bid = -2;
        for (var i = 0; i < searcheUrlParams.length; i ++) {
            if (searcheUrlParams[i].split("=")[0] == "bid"){
                try {
                    bid = searcheUrlParams[i].split("=")[1];
                }catch (e) {
                    console.log(e)
                }
            }
    
        }
        axios({
            method: "get",
            url: "/queryCommentsByBlogId?bid=" + bid
        }).then((res) => {
            let temp = [];
            temp = res.data.data;
            console.log(temp)
            this.commentList = temp;
            for(var i = 0 ; i < this.commentList.length; i++ ) {
                if (this.commentList[i].parent > -1) {
                    this.commentList[i].options = "回复@" + this.commentList[i].parent_name
                }
            }
        })

        axios({
            method: "get",
            url: "/queryBlogCommentsCount?bid=" + bid
        }).then((res) => {
            this.total = res.data.data[0].count
        })
    }
})